package com.njitt.bysj.utils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.njitt.bysj.model.BomModel;
import com.njitt.bysj.srevice.ProjectService;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 导入excel
 * @author: wyj
 * @time: 2022/4/16 23:37
 */
public class ExcelListener extends AnalysisEventListener<BomModel> {
    // 分析事件监听器
    private List<BomModel> list = new ArrayList<>();
    private static final int BATCH_COUNT = 200;
    private ProjectService service;
    public ExcelListener(ProjectService projectService) {
        this.service = projectService;
    }

    @Override
    public void invoke(BomModel bomModel, AnalysisContext analysisContext) {
        System.out.println("解析到一条数据：=========" + bomModel);
        list.add(bomModel);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }
    private void saveData() {
        service.insertBatchList(list);
        System.out.println("==========开始存入数据库");
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }
}
